﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIPanel_Start : UIPanelBase {
    Transform panelRoot;
    Transform btnSetting;
    Transform btnQuit;
    Transform btnGameMode;
   
    Transform selectPictureScrollView;
    GameMode selectGM;

    public string uiPrefabName
    {
        get
        {
            Debug.LogError(typeof(UIPanel_Start).Name);
            return  typeof(UIPanel_Start).Name; 
        }
    }

    public override void OnInit(Transform root)
    {
        base.OnInit(root);
        panelRoot = root;
        InitGameObj();
        InitEvent();

        NetUtil.instance.Request(UrlPath.versionUrl, VersionCheck, VersionCheckTimeOut);

    }

    void VersionCheck(WWW w)
    {
        Debug.Log("new version:"+w.text);
    }

    void VersionCheckTimeOut()
    {
        Debug.LogError("VersionCheckTimeOut");
    }

    void InitGameObj()
    {
        btnSetting = panelRoot.Find("btn_setting");
        btnQuit = panelRoot.Find("btn_quit");
        btnGameMode = panelRoot.Find("Panel_btn_list");
        btnGameMode.gameObject.SetActive(true);
        selectPictureScrollView = panelRoot.Find("Scroll View");

        selectPictureScrollView.gameObject.SetActive(false);

    }
    void InitEvent()
    {
        Button btn_setting = btnSetting.GetComponent<Button>();
        btn_setting.onClick.RemoveAllListeners();
        btn_setting.onClick.AddListener(()=> {
            UIManager.ShowUI<UIPanel_Setting>();
        });

        Button btn_quit = btnQuit.GetComponent<Button>();
        btn_quit.onClick.RemoveAllListeners();
        btn_quit.onClick.AddListener(() => {
            UIManager.ShowUI<UIPanel_Quit>();
        });

        for (int i = 0; i < btnGameMode.childCount; i++)
        {
            int index = i;
            Button btnStartTem = btnGameMode.GetChild(index).GetComponent<Button>();
            btnStartTem.onClick.RemoveAllListeners();
            btnStartTem.onClick.AddListener(()=> {
                StartGame((GameMode)index);
            });
            UIHoverListener.RegHoverEvent(btnStartTem.gameObject, (enterPd) => {
                HandleModeBtnHoverEvent(btnStartTem.transform,true);
            }, (exitPd) => {
                HandleModeBtnHoverEvent(btnStartTem.transform, false);
            });
        }
    }

    void HandleModeBtnHoverEvent(Transform item, bool isEnterOrExit)
    {
        Transform hoverImg = item.Find("img_hover");
        hoverImg.gameObject.SetActive(isEnterOrExit);
    }

    void StartGame(GameMode gm)
    {
        selectGM = gm;
        ToSelectPicture();
    }

    void ToSelectPicture()
    {
        btnGameMode.gameObject.SetActive(false);
        selectPictureScrollView.gameObject.SetActive(true);

        Transform btnBackToSelectMode = selectPictureScrollView.Find("btn_back");
        Button btn_back_to_select_mode = btnBackToSelectMode.GetComponent<Button>();
        btn_back_to_select_mode.onClick.RemoveAllListeners();
        btn_back_to_select_mode.onClick.AddListener(()=> {
            btnGameMode.gameObject.SetActive(true);
            selectPictureScrollView.gameObject.SetActive(false);
        });
        Texture2D[] t2ds = Resources.LoadAll<Texture2D>("texture_puzzle");
        InitPictureList(t2ds);
    }

    void InitPictureList(Texture2D[] t2ds)
    {
        //GameMain.instance.StartCoroutine();
        ScrollRect sr = selectPictureScrollView.GetComponent<ScrollRect>();
        Transform contentRoot = sr.content;
        GameObject itemPrefab = contentRoot.GetChild(0).gameObject;
        int maxCycleLent = Mathf.Max(t2ds.Length, contentRoot.childCount);
        for (int i = 0; i < maxCycleLent; i++)
        {
            GameObject itemObj = null;
            if (i < contentRoot.childCount)
            {
                itemObj = contentRoot.GetChild(i).gameObject;
            }
            else
            {
                itemObj = GameObject.Instantiate<GameObject>(itemPrefab);
                itemObj.transform.SetParent(contentRoot,false);
            }

            if (i < t2ds.Length)
            {
                itemObj.gameObject.SetActive(true);
                Image image = itemObj.GetComponent<Image>();
                Texture2D t2dTem = t2ds[i];
                image.sprite = Sprite.Create(t2ds[i],new Rect(0,0, t2dTem.width, t2dTem.height),new Vector2(.5f,.5f));
                Button btnItem = itemObj.GetComponent<Button>();
                btnItem.onClick.RemoveAllListeners();
                btnItem.onClick.AddListener(()=> {
                    OnSelectPicture(t2dTem);
                }) ;
            }
            else
            {
                itemObj.gameObject.SetActive(false);
            }
        }


    }

    

    void OnSelectPicture(Texture2D t2d)
    {
        UIManager.CloseUI<UIPanel_Start>();
        UIManager.ShowUI<UIPanel_Game>(() => {
            UIPanel_Game uiPanel_Game = UIManager.GetUIPanelByType<UIPanel_Game>();
            uiPanel_Game.StartGame(t2d, selectGM);
        });
    }


  

    public override void Update()
    {
        base.Update();
    }
    public override void OnClose()
    {
        base.OnClose();
    }
}
